package com.lgl.mes.technology.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lgl.mes.basedata.entity.MaterialExcel;
import com.lgl.mes.basedata.entity.SpMaterile;
import com.lgl.mes.basedata.service.impl.FileNotEmptyException;
import com.lgl.mes.technology.entity.BomExcel;
import com.lgl.mes.technology.entity.SpBom;
import com.lgl.mes.technology.mapper.SpBomMapper;
import com.lgl.mes.technology.service.ISpBomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwenze.poi.ExcelKit;
import com.wuwenze.poi.handler.ExcelReadHandler;
import com.wuwenze.poi.pojo.ExcelErrorField;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  BOM服务实现类
 * </p>
 *
 * @author lgl
 * @since 2020-03-28
 */
@Service
public class SpBomServiceImpl extends ServiceImpl<SpBomMapper, SpBom> implements ISpBomService {

    @Override
    public String LocalImport(MultipartFile file) {
        String message = "";
        List<BomExcel> successList = new ArrayList();
        List<Map<String, Object>> errorList = new ArrayList();
        if (file.isEmpty()) {
            throw new FileNotEmptyException("上传文件不能为空");
        }
        try {
            ExcelKit.$Import(BomExcel.class)
                    .readXlsx(file.getInputStream(), new ExcelReadHandler<BomExcel>() {
                        @Override
                        public void onSuccess(int i, int i1, BomExcel bomExcel) {
                            successList.add(bomExcel); // 单行读取成功，加入入库队列。
                        }
                        @Override
                        public void onError(int sheetIndex, int rowIndex, List<ExcelErrorField> list) {
                            // 读取数据失败，记录了当前行所有失败的数据
                            Map<String, Object> map = new HashMap<>();
                            map.put("sheetIndex", sheetIndex);
                            map.put("rowIndex", rowIndex);
                            map.put("errorFields", list);
                            errorList.add(map);
                        }
                    });


            successList.stream().forEach(item ->{
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(item);
                String materielCode  = jsonObject.getString("materielCode");
                QueryWrapper<SpBom> wrapper=new QueryWrapper<>();
                wrapper.eq("materiel_code", materielCode); //按materiel_code  而不是 bom_code
                if (this.list(wrapper).size()==0) {
                    SpBom spBom = new SpBom(jsonObject);
                    this.baseMapper.insert(spBom);
                }
            });
        } catch (Exception e) {
            message = e.getMessage();
            e.printStackTrace();
        }
        return message;
    }



}
